//
//  XS_LearnHeaderView.swift
//  XueDao
//
//  Created by Mac on 2020/12/14.
//

import UIKit

protocol XS_LearnHeaderViewDelegate {
    func learnHeaderView(learnHeaderView : XS_LearnHeaderView, didClickVipView : UIView)
}

class XS_LearnHeaderView: UIView {
    
    var delegate : XS_LearnHeaderViewDelegate?
    var _model : XS_LearnTimeModel!
    public var model : XS_LearnTimeModel!{
        set{
            _model = newValue
            //设置数据
            timeView.model = newValue
            
            if XS_Singleton.shared.infoModel?.userBO != nil {
                if XS_Singleton.shared.infoModel?.userBO.userTypeEnum() == XS_UserType.vip {
                    vipView.openTitleLab.isHidden = true
                    vipView.openVipBtn.isHidden = true
                    
                    vipView.vipTitleLab.isHidden = false
                    vipView.vipTimeLab.isHidden = false
                    vipView.vipInfoBtn.isHidden = false
                    
                    vipView.vipTimeLab.text = String.init((XS_Singleton.shared.infoModel?.userBO.vipExpirationTime)!) + " 到期"
                } else {
                    vipView.openTitleLab.isHidden = false
                    vipView.openVipBtn.isHidden = false
                    
                    vipView.vipTitleLab.isHidden = true
                    vipView.vipTimeLab.isHidden = true
                    vipView.vipInfoBtn.isHidden = true
                }
            }
        }
        get{
            return _model
        }
    }
    
    @objc func vipViewAction(){
        if delegate != nil {
            delegate?.learnHeaderView(learnHeaderView: self, didClickVipView: vipView)
        }
    }

    lazy var vipView : XS_VIPView = {
        let vipView = Bundle.main.loadNibNamed("XS_VIPView", owner: nil, options: nil)?.last as! XS_VIPView
        vipView.openVipBtn.addTarget(self, action: #selector(vipViewAction), for: .touchUpInside)
        vipView.vipInfoBtn.addTarget(self, action: #selector(vipViewAction), for: .touchUpInside)
        return vipView
    }()
    
    lazy var timeView : XS_LearnTimeView = {
        let timeView = XS_LearnTimeView.init(frame: CGRect.init(x: 0, y: 96, width: .screenWidth, height: 313))
        return timeView
    }()
    
    lazy var allView : XS_LearnAllView = {
        let allView = XS_LearnAllView.init(frame: CGRect.init(x: 0, y: 96 + 313, width: .screenWidth, height: 56))
        return allView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(vipView)
        addSubview(timeView)
        addSubview(allView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
